/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.system.service.impl;

import com.alien.admin.modules.system.domain.RolesMenus;
import com.alien.admin.utils.ValidationUtil;
import com.alien.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.system.repository.RolesMenusRepository;
import com.alien.admin.modules.system.service.RolesMenusService;
import com.alien.admin.modules.system.service.dto.RolesMenusDto;
import com.alien.admin.modules.system.service.dto.RolesMenusQueryCriteria;
import com.alien.admin.modules.system.service.mapstruct.RolesMenusMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.alien.admin.utils.PageUtil;
import com.alien.admin.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-10-29
**/
@Service
@RequiredArgsConstructor
public class RolesMenusServiceImpl implements RolesMenusService {

    private final RolesMenusRepository rolesMenusRepository;
    private final RolesMenusMapper rolesMenusMapper;

    @Override
    public Map<String,Object> queryAll(RolesMenusQueryCriteria criteria, Pageable pageable){
        Page<RolesMenus> page = rolesMenusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(rolesMenusMapper::toDto));
    }

    @Override
    public List<RolesMenusDto> queryAll(RolesMenusQueryCriteria criteria){
        return rolesMenusMapper.toDto(rolesMenusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RolesMenusDto findById(String env) {
        RolesMenus rolesMenus = rolesMenusRepository.findById(env).orElseGet(RolesMenus::new);
        ValidationUtil.isNull(rolesMenus.getEnv(),"RolesMenus","env",env);
        return rolesMenusMapper.toDto(rolesMenus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RolesMenusDto create(RolesMenus resources) {
        resources.setEnv(IdUtil.simpleUUID());
        return rolesMenusMapper.toDto(rolesMenusRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RolesMenus resources) {
        RolesMenus rolesMenus = rolesMenusRepository.findById(resources.getEnv()).orElseGet(RolesMenus::new);
        ValidationUtil.isNull( rolesMenus.getEnv(),"RolesMenus","id",resources.getEnv());
        rolesMenus.copy(resources);
        rolesMenusRepository.save(rolesMenus);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String env : ids) {
            rolesMenusRepository.deleteById(env);
        }
    }

    @Override
    public void download(List<RolesMenusDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RolesMenusDto rolesMenus : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
